package com.ruoyi.biz.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.biz.domain.BizInvoice;
import com.ruoyi.biz.mapper.BizInvoiceMapper;
import com.ruoyi.biz.model.resp.BizInvoiceHeaderSimpleResp;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;

import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.mapper.BizInvoiceHeaderMapper;
import com.ruoyi.biz.domain.BizInvoiceHeader;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


/**
 * 发票抬头Service业务层处理
 *
 * @author tianl
 * @date 2024-12-31
 */
@Service
public class BizInvoiceHeaderService extends ServiceImpl<BizInvoiceHeaderMapper, BizInvoiceHeader> {
    @Autowired
    private BizInvoiceMapper bizInvoiceMapper;

    /**
     * 分页查询发票抬头
     */
    public IPage<BizInvoiceHeader> listPage(BizInvoiceHeader req) {
        return baseMapper.list(PageUtils.page(), req);
    }

    /**
     * 查询发票抬头列表
     */
    public List<BizInvoiceHeader> list(BizInvoiceHeader req) {
        return baseMapper.list(req);
    }

    public List<BizInvoiceHeaderSimpleResp> listAll(BizInvoiceHeader req) {
        return baseMapper.listAll(req);
    }

    /**
     * 根据ID查询
     */
    public BizInvoiceHeader selectById(Long id) {
        return baseMapper.selectById(id);
    }

    public BizInvoiceHeader findByName(String title) {
        LambdaQueryWrapper<BizInvoiceHeader> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(i -> i.eq(BizInvoiceHeader::getHeaderTitle, title));
        List<BizInvoiceHeader> list = list(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 新增修改发票抬头
     */
    @Transactional(rollbackFor = Exception.class)
    public void beforeSave(BizInvoiceHeader bizInvoiceHeader) {
        if (bizInvoiceHeader.getId() != null) {
            bizInvoiceHeader.setUpdateTime(DateUtils.getNowDate());
        } else {
            bizInvoiceHeader.setCreateTime(DateUtils.getNowDate());
            bizInvoiceHeader.setUpdateTime(DateUtils.getNowDate());
        }
        saveOrUpdate(bizInvoiceHeader);
    }

    /**
     * 批量删除发票抬头
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Long[] ids) {
        for (Long id : ids) {
            LambdaQueryWrapper<BizInvoice> wrapper = new LambdaQueryWrapper<>();
            wrapper.and(i -> i.eq(BizInvoice::getHeaderId, id));
            long count = bizInvoiceMapper.selectCount(wrapper);
            if (count > 0) {
                BizInvoiceHeader header = selectById(id);
                throw new ServiceException(header.getHeaderTitle() + "已有发票使用，不允许删除！");
            }
        }
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

}
